﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：马毅 
    //
    // 文件名：DistributionRelationByCom.cs
    // 文件功能描述：按地区查看配送关系
    // 创建标识：马毅2009-05-18
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model;
using System.Data;
using HZ.DrugTrade.Model.Enum;
namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{
    public partial class ProductCompanySubArea1 : BasePage
    {
        public ProductCompanySubArea1()
        {
            this.ModuleNo = Module.分配送管理.ToString();
        }

        IBLL_Distribution id = Factory.BLL_Distribution();
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();

        string procureCatalogId = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            procureCatalogId = Request.QueryString["PID"];
            ViewState["v_procureCatalogId"] = procureCatalogId;
            if (!IsPostBack)
            {
                Banding();//绑定
            }
        }
        /// <summary>
        /// 绑定
        /// </summary>
        private void Banding()
        {
            Model.View.ProcureCatalog procureCatalog = ipb.GetProcureCatalogByID(procureCatalogId);
            lblCompany_SC.Text = procureCatalog.CompanyName_SC;
            lblCompany_ZB.Text = procureCatalog.CompanyName_TB;
            lblGoodsName.Text = procureCatalog.GoodsName;
            lblMaterial.Text = procureCatalog.Material;
            lblOutlookc.Text = procureCatalog.Outlookc;
            lblUnit.Text = procureCatalog.Unit;

            HZ.DrugTrade.Model.Query.AreaDistribution aDistributionRelation = new HZ.DrugTrade.Model.Query.AreaDistribution();
            aDistributionRelation.ProcureCatalogID = ViewState["v_procureCatalogId"].ToString();
            aDistributionRelation.SortFields = "CompanyName_PS asc";
            aDistributionRelation.CompanyID_Main = CurrentAccount.GetUser().Org.OrgID;
            int totalCount = 0;
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            IList<HZ.DrugTrade.Model.View.AreaDistribution> distibutionRelationList = id.GetAreaDistribution(aDistributionRelation, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            gvwProduceCatalog.DataSource = distibutionRelationList;
            gvwProduceCatalog.DataBind();
            AspNetPager1.RecordCount = totalCount;
        }


        /// <summary>
        /// 获取默认选中的地区编号串
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultCity = "";
            string defaultProvince = "";

            Model.Area aArea = CurrentAccount.GetCurrentDeployArea();  //获取当前实施地区的地区

            defaultProvince = aArea.AreaID;

            string strArea = "";
            //显示默认地区
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;
            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"] + "\"";
            }

            return strArea;
        }

        /// <summary>
        /// 保存地区和和企业
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void HzButton1_Click(object sender, EventArgs e)
        {

            HZ.DrugTrade.Model.PartDistributionLog aPartDistributionLog = new HZ.DrugTrade.Model.PartDistributionLog();
            aPartDistributionLog.ProcureCatalogID = ViewState["v_procureCatalogId"].ToString();
            aPartDistributionLog.LastCompanyID = CurrentAccount.GetUser().Org.OrgID;
            if (new HZ.Common.ConvertHelper(companyId.Value).String == "")
            {
                HZ.Web.WebUtil.Alert("配送企业未选择！", this); return;
            }
            if (new HZ.Common.ConvertHelper(companyId.Value).String == CurrentAccount.GetUser().Org.OrgID)
            {
                HZ.Web.WebUtil.Alert("不能添加本企业！", this); return;
            }
            aPartDistributionLog.NowCompanyID = companyId.Value.Trim();
            aPartDistributionLog.AreaID = GetAreaID();
            aPartDistributionLog.PartDistributionSerialID = Guid.NewGuid().ToString();
            List<HZ.DrugTrade.Model.PartDistributionLog> listPartDistributionLog = new List<PartDistributionLog>();
            listPartDistributionLog.Add(aPartDistributionLog);
            int nResurt = id.AddPartDistributionLog(listPartDistributionLog);
            if (nResurt == 1)
            {
                HZ.Web.WebUtil.Alert("保存成功！", this);
                this.txtCompany.Text = "";
                this.companyId.Value = "";
                //
                Banding();
            }
            else if (nResurt == 2)
            {
                HZ.Web.WebUtil.Alert("已存在指定分配送！", this);
                this.txtCompany.Text = "";
                this.companyId.Value = "";
            }
            else
            {
                HZ.Web.WebUtil.Alert("数据异常！", this);
                this.txtCompany.Text = "";
                this.companyId.Value = "";
            }

        }
        /// <summary>
        /// 获取地区ID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";

            //首次进入使用系统部署所在区域ID
            Model.Area aArea = CurrentAccount.GetCurrentDeployArea();  //获取当前实施地区的地区
            areaId = aArea.AreaID;

            //优先获取城市，再次省份
            if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCity"];
            }
            else if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"];
            }

            return areaId;
        }

        protected void btn_Del(object sender, EventArgs e)
        {

            HZ.Web.HzLinkButton btn = sender as HZ.Web.HzLinkButton;
            string str = btn.CommandArgument.ToString();
            string[] array = str.Split(',');
            string isUsing = array[3];
            if (isUsing != false.ToString())
            {
                IList<HZ.DrugTrade.Model.AreaDistribution> distibutionRelationList = new List<HZ.DrugTrade.Model.AreaDistribution>();
                HZ.DrugTrade.Model.AreaDistribution aAreaDistribution = new HZ.DrugTrade.Model.AreaDistribution();
                aAreaDistribution.ProcureCatalogID = array[0];
                aAreaDistribution.CompanyID_PS = array[1];
                aAreaDistribution.AreaID = array[2];
                aAreaDistribution.CompanyID_Main = CurrentAccount.GetUser().Org.OrgID;
                distibutionRelationList.Add(aAreaDistribution);
                if (id.DeleteAreaDistribution_Batch(distibutionRelationList) == 1)
                {
                    PageInfo("删除成功！", Request.RawUrl);

                }
            }
            else
            {
                PageError("此医疗机构正在使用中，无法删除！", Request.RawUrl);
            }

        }
        /// <summary>
        /// 绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HZ.Web.HzLinkButton btn = e.Row.FindControl("lbtnDel") as HZ.Web.HzLinkButton;
                HZ.DrugTrade.Model.View.AreaDistribution item = e.Row.DataItem as HZ.DrugTrade.Model.View.AreaDistribution;
                btn.CommandArgument = string.Format("{3},{2},{1},{0}", item.IsUsing, item.AreaID, item.CompanyID_PS, item.ProcureCatalogID);

            }
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            Banding();
        }

    }

}
